﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Global;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Clipper.Model.DataBaseModel;
using Clipper.ClipperWindow;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserDetectResult.xaml 的交互逻辑
    /// </summary>
    public partial class UserDetectResult : UserControl
    {
        #region 变量
        BLL.DataBaseBLL.CupDetectResultInfomation mCupbll = new BLL.DataBaseBLL.CupDetectResultInfomation();
        // BLL.DataBaseBLL.CupDetectResultInfomation CupBll = new BLL.DataBaseBLL.CupDetectResultInfomation();
        BLL.DetectResult mDetectResultBll = new BLL.DetectResult();
        BLL.DetectItems mDetectItemBll = new BLL.DetectItems();
        //ObservableCollection<Model.DetectResult> mResultList = null;

        ObservableCollection<Model.DetectItems> mDetectItems = null;
        public UserDetectResult()
        {
            InitializeComponent();
        }

        #endregion

        public void DataBind()
        {
            if (cboDetectItem.ItemsSource == null)
            {
                mDetectItems = mDetectItemBll.GetModelList("");
                //ICollectionView mViewCollection = CollectionViewSource.GetDefaultView(mDetectItems);
                //mViewCollection.GroupDescriptions.Add(new PropertyGroupDescription("DetectVariety.typeName"));
                cboDetectItem.ItemsSource = mDetectItems;


                dtpStart.SelectedDate = DateTime.Now;
                dtpEnd.SelectedDate = DateTime.Now;

                //  BLL.DataBaseBLL.CupDetectResultInfomation mCupbll = new BLL.DataBaseBLL.CupDetectResultInfomation();
                ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> mCupList = mCupbll.GetModelList();

                if (mCupList != null)
                {
                    gridDetectResult.ItemsSource = mCupList;
                }
            }
        }

        /// <summary>
        /// 查询结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string strStart = null;
            string strEnd = null;
            int? sampleStart = null;
            int? sampleEnd = null;
            string detectItem = null;

            if (chkDate.IsChecked.Value)
            {
                strStart = dtpStart.SelectedDate.Value.ToString("yyyy-MM-dd");
                strEnd = dtpEnd.SelectedDate.Value.ToString("yyyy-MM-dd");
            }
            if (chkSampleId.IsChecked.Value)
            {
                sampleStart = txtSampleStart.Number;
                sampleEnd = txtSampleEnd.Number;
            }
            if (chkDetectItem.IsChecked.Value)
            {
                if (cboDetectItem.SelectedValue != null)
                {
                    detectItem = cboDetectItem.SelectedValue.ToString();
                }
            }
            //       mResultList = mDetectResultBll.GetDetectResult(strStart, strEnd, sampleStart, sampleEnd, detectItem, chkFailure.IsChecked.Value);
            //    BLL.DataBaseBLL.CupDetectResultInfomation mCupbll = new BLL.DataBaseBLL.CupDetectResultInfomation();
            //ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> mCupList = mCupbll.GetModelList();

            ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> mCupList = mCupbll.GetCupDetectResultInformation(strStart, strEnd, sampleStart, sampleEnd, detectItem, chkFailure.IsChecked.Value);

            if (mCupList != null)
            {
                gridDetectResult.ItemsSource = mCupList;
            }

        }
        //输出到Excel
        private void btnToExcel_Click(object sender, RoutedEventArgs e)
        {

        }
        //重新计算浓度
        private void btnSaveReCalc_Click(object sender, RoutedEventArgs e)
        {

        }
        //删除记录
        private void btnDel_Click(object sender, RoutedEventArgs e)
        {

            WinDeleteNotice winDelete = new WinDeleteNotice();
            winDelete.ShowDialog();

            //判断是否可以执行一下操作
            if (ServiceProvider.Monitor.WinDiolageDeleteEnum == Model.EnumModel.WinDiolageDeleteEnum.OPEN)
            {
                ServiceProvider.Monitor.WinDiolageDeleteEnum = Model.EnumModel.WinDiolageDeleteEnum.IDLE; //更改一下变量
                Model.DataBaseModel.CupDetectResultInformation cupDetectResult = gridDetectResult.SelectedItem as CupDetectResultInformation;
                if (mCupbll.DeleteCupDetectResultById(cupDetectResult))
                {
                    //弹框显示
                    WinDeleteSuccess win = new WinDeleteSuccess();
                    win.ShowDialog();
                    //更新数据显示
                    ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> mCupList = mCupbll.GetModelList();
                    if (mCupList != null)
                    {
                        gridDetectResult.ItemsSource = mCupList;
                    }
                }

                //ServiceProvider.Monitor.WinDiolageDeleteEnum = Model.EnumModel.WinDiolageDeleteEnum.IDLE; //更改一下变量
                //ObservableCollection<CupDetectResultInformation> cupDetectResult = gridDetectResult.SelectedItems as ObservableCollection<CupDetectResultInformation>;
                //for (int i = 0; i < cupDetectResult.Count; i++)
                //{
                //    if (mCupbll.DeleteCupDetectResultById(cupDetectResult[i]))
                //    {
                //        //弹框显示
                //        WinDeleteSuccess win = new WinDeleteSuccess();
                //        win.ShowDialog();
                //        //更新数据显示
                //        ObservableCollection<Model.DataBaseModel.CupDetectResultInformation> mCupList = mCupbll.GetModelList();
                //        if (mCupList != null)
                //        {
                //            gridDetectResult.ItemsSource = mCupList;
                //        }
                //    }
                //}

            }

        }
        //统计分析
        private void btnStatistics_Click(object sender, RoutedEventArgs e)
        {

        }

        private void detectResult_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (ServiceProvider.SelfCheckPass)
            {
                if (gridDetectResult.SelectedItem != null)
                {
                    WinResultShow win = new WinResultShow();
                    win.Tag = gridDetectResult.SelectedItem;
                    //win.Closed += new EventHandler(win_Closed);
                    win.ShowDialog();
                }
            }
            else
            {
                UIHelper.ShowKeyMessage("PleaseSelfCheck");
            }
        }
    }
}
